package com.ruoyi.data.tdengine.utils;

import cn.hutool.core.lang.Console;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 发送curl命令工具
 * author: zhouzhou
 */
@Configuration
@Slf4j
public class CurlUtils {
    private static String TDengineUrl;
    private static String TDengineRoot;

    @Value("${tdengine.url}")
    public void setTDengineUrl(String TDengineUrl) {
        CurlUtils.TDengineUrl = TDengineUrl;
    }
    @Value("${tdengine.root}")
    public void setTDengineRoot(String TDengineRoot) {
        CurlUtils.TDengineRoot = TDengineRoot;
    }

    public static boolean isWindows(){
        String osName = System.getProperties().getProperty("os.name");
        return !osName.equals("Linux");
    }

    /**
     * @param sql 执行的sql语句
     * @return 请求结果
     */
    public static JSONObject sendRestFulSql(String sql){
        log.debug("执行sql:{}",sql);
        String url = "Basic "+new String(new Base64().encode(CurlUtils.TDengineRoot.getBytes()));
        String response = HttpRequest.post("http://"+CurlUtils.TDengineUrl+"/rest/sql")
                .header(Header.AUTHORIZATION, url)
                .body(sql)
                .timeout(20000)
                .execute().body();
        return JSONObject.parseObject(response);
    }
}
